/**
 * 方案模型类
 */
angular.module('myServices', []).factory('$localstorage', ['$window',
	function($window) {
		return {
			set: function(key, value) {
				$window.localStorage[key] = value;
			},
			get: function(key, defaultValue) {
				return $window.localStorage[key] || defaultValue;
			},
			setObject: function(key, value) {
				$window.localStorage[key] = JSON.stringify(value);
			},
			getObject: function(key) {
				return JSON.parse($window.localStorage[key] || '{}');
			},
			setList: function(key, value) {
				$window.localStorage[key] = JSON.stringify(value);
			},
			getList: function(key) {
				return JSON.parse($window.localStorage[key] || '[]');
			},
			clear: function() {
				$window.localStorage.clear();
			}
		}
	}
]).factory('Util', function($ionicLoading) {
	return {
		toast: function(message, time) {
			$ionicLoading.show({
				template: '<span class="icon spin ion-ios7-information-outline"></span> ' + message,
				noBackdrop: true,
				duration: time
			});
		}
	}
}).factory('Image', ['$http', '$q',
	function($http, $q) {
		return {
			get: function(field, p) {
				var deferred = $q.defer();
				var url = _GLOBAL.api;
				$http.get(url, {
					params: {
						"a": "get",
						"f": field,
						"p": p,
					}
				}).success(function(ret) {
					deferred.resolve(ret);
				}).error(function() {

				});
				return deferred.promise;
			},
			getCatalog: function() {
				var deferred = $q.defer();
				var url = _GLOBAL.api + "?a=getCatalog";
				$http.get(url, {
					cache: true
				}).success(function(ret) {
					deferred.resolve(ret);
				});
				return deferred.promise;
			},
			getSlider: function() {
				var deferred = $q.defer();
				var url = _GLOBAL.api + "?a=getSlider";
				$http.get(url).success(function(ret) {
					deferred.resolve(ret);
				});
				return deferred.promise;
			},
			add: function(form) {
				var deferred = $q.defer();
				var url = _GLOBAL.api;
				$http.get(url, {
					"a": add,
					"name": form.name,
					"url": form.url
				}).success(function(ret) {
					deferred.resolve(ret);
				});
				return deferred.promise;
			}
		}
	}
]);